package com.cadre.common.utils.oldutil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* loaded from: J4SqlLiteUtils.class */
public class J4SqlLiteUtils {
    private Connection connection = null;
    private PreparedStatement pstat = null;
    private ResultSet rst = null;

    public Connection getConnection(String path) {
        try {
            Class.forName("org.sqlite.JDBC");
            this.connection = DriverManager.getConnection("jdbc:sqlite:" + path);
            System.out.println("[SQL LITE]: 获取到sqlite的jdbc连接： " + this.connection);
        } catch (Exception e) {
            System.err.println("[SQL LITE]: 获取sqlite的jdbc连接失败");
            System.err.println(e.getMessage());
        } finally {
            close();
        }
        return this.connection;
    }

    public int execute(String sql, Object... args) {
        try {
            int executeUpdate = 0;
            try {
                System.out.println("[SQL EXECUTE]: " + sql);
                this.pstat = this.connection.prepareStatement(sql);
                if (args.length > 0) {
                    int len = args.length;
                    for (int i = 0; i < len; i++) {
                        this.pstat.setObject(i + 1, args[i]);
                    }
                }
                executeUpdate = this.pstat.executeUpdate();
                this.rst = this.pstat.getGeneratedKeys();
                if (this.rst != null) {
                }
                System.out.println("[SQL EXECUTE RESULT]: " + executeUpdate);
                close();
            } catch (Exception e) {
                e.printStackTrace();
                close();
            }
            return executeUpdate;
        } catch (Throwable th) {
            close();
            throw th;
        }
    }

    public Map unique(String sql) {
        try {
            Map result = null;
            try {
                System.out.println("[SQL UNIQUE]: " + sql);
                this.pstat = this.connection.prepareStatement(sql);
                this.rst = this.pstat.executeQuery();
                ResultSetMetaData metaData = this.rst.getMetaData();
                int cols = metaData.getColumnCount();
                if (this.rst.next()) {
                    result = new HashMap();
                    for (int i = 0; i < cols; i++) {
                        result.put(metaData.getColumnName(i + 1), this.rst.getObject(i + 1));
                    }
                }
                System.out.println("[SQL UNIQUE RESULT]: " + result);
                close();
            } catch (Exception e) {
                e.printStackTrace();
                close();
            }
            return result;
        } catch (Throwable th) {
            close();
            throw th;
        }
    }

    public List list(String sql) {
        try {
            List results = new ArrayList();
            try {
                System.out.println("[SQL LIST]: " + sql);
                this.pstat = this.connection.prepareStatement(sql);
                this.rst = this.pstat.executeQuery();
                ResultSetMetaData metaData = this.rst.getMetaData();
                int cols = metaData.getColumnCount();
                while (this.rst.next()) {
                    HashMap hashMap = new HashMap();
                    for (int i = 0; i < cols; i++) {
                        hashMap.put(metaData.getColumnName(i + 1), this.rst.getObject(i + 1));
                    }
                    results.add(hashMap);
                }
                System.out.println("[SQL LIST]: " + results);
                close();
            } catch (Exception e) {
                e.printStackTrace();
                close();
            }
            return results;
        } catch (Throwable th) {
            close();
            throw th;
        }
    }

    public int count(String tableName) {
        return count(tableName, null);
    }

    public int count(String tableName, String where) {
        try {
            int count = 0;
            try {
                System.out.println("[SQL Count]: " + ("select count(*) from " + tableName + " " + (where == null ? "" : where)));
                this.pstat = this.connection.prepareStatement("select count(*) from " + tableName + " " + (where == null ? "" : where));
                this.rst = this.pstat.executeQuery();
                if (this.rst.next()) {
                    count = this.rst.getInt(1);
                }
                System.out.println("[SQL Count Result]: " + count);
                close();
            } catch (Exception e) {
                e.printStackTrace();
                close();
            }
            return count;
        } catch (Throwable th) {
            close();
            throw th;
        }
    }

    private void close() {
        if (this.rst != null) {
            try {
                this.rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (this.pstat != null) {
            try {
                this.pstat.close();
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
        }
    }

    public void relase() {
        if (this.connection != null) {
            try {
                this.connection.close();
                System.out.println("[SQL LITE]: 关闭connection连接");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
